package com.dreamthen.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;

/**
 * @Author zengms
 * @Date 2023-10-29 09:20:32
 **/
@Component
@Aspect
public class LogAspect {

    @Pointcut(value = "execution(* com.dreamthen.service.*.*(..))")
    public void myPointCut(){

    }
    //前置通知
    @Before(value = "myPointCut()")
    public void before(JoinPoint joinPoint){
        String methodName = joinPoint.getSignature().getName();
        Object[] args = joinPoint.getArgs();
        if(null == args){
            System.out.println("前置通知-方法"+methodName+"对应参数：空");
        }else{
            System.out.println("前置通知-方法"+methodName+"对应参数："+Arrays.asList(args));
        }
    }

    //后置通知
    @After(value = "myPointCut()")
    public void after(JoinPoint joinPoint){
        String methodName = joinPoint.getSignature().getName();
        Object[] args = joinPoint.getArgs();
        if(null == args){
            System.out.println("后置通知-方法"+methodName+"对应参数：空");
        }else{
            System.out.println("后置通知-方法"+methodName+"对应参数："+Arrays.asList(args));
        }
    }

    //后置返回通知
    @AfterReturning(value = "myPointCut()",returning = "result")
    public Object afterReturn(JoinPoint joinPoint,Object result){
        String methodName = joinPoint.getSignature().getName();
        Object[] args = joinPoint.getArgs();
        if(null == args){
            System.out.println("后置返回-方法"+methodName+"对应参数：空");
        }else{
            System.out.println("后置返回-方法"+methodName+"对应参数："+Arrays.asList(args));
        }
        System.out.println("后置返回-结果："+result);
        return result;
    }
    //后置异常通知
    @AfterThrowing(value = "myPointCut()",throwing = "exp")
    public Object afterThrow(JoinPoint joinPoint,Exception exp){
        String methodName = joinPoint.getSignature().getName();
        Object[] args = joinPoint.getArgs();
        if(null == args){
            System.out.println("后置异常-方法"+methodName+"对应参数：空");
        }else{
            System.out.println("后置异常-方法"+methodName+"对应参数："+Arrays.asList(args));
        }
        System.out.println("后置异常-信息"+exp);
        return exp;
    }

}
